package com.dyzs.activity.word;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.dyzs.activity.CommonListActivity;
import com.dyzs.activity.R;
import com.dyzs.util.CityUtil;
import com.dyzs.util.Constants;
import com.dyzs.util.DatabaseOperation;
import com.dyzs.util.Pager;
public class WordCollectionActivity extends CommonListActivity {
	
	private Spinner proviceSP;	
	private ArrayAdapter<String> proviceAdapter;
	private Map<String, String[]> proviceMap = null;
	private DatabaseOperation dbOperator;
	
	private String searchTitle="",searchCode="";
	private List list;	
	private static final int[] menu_image_array = { R.drawable.up,R.drawable.down};
	private static final String[] menu_text_array = { "上一页", "下一页"};	

	
	
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.word_collection);
		this.setTitle("收藏导游词");
		emptyTV=(TextView)findViewById(android.R.id.empty);
		setSp();
		setGV();	
	}
		
	protected void onResume() {
		super.onResume();
		search();
	}
	
	private void setSp(){
		proviceSP=(Spinner) this.findViewById(R.id.word_collection_provice);
		proviceMap = CityUtil.getProvice(this);
		
		proviceAdapter = new ArrayAdapter<String>(this,	android.R.layout.simple_spinner_item, proviceMap.get("proviceNames"));
		proviceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		proviceSP.setAdapter(proviceAdapter);
		proviceSP.setSelection(0);
	}
	
	
	public void onClick(View v) {
		int t = proviceSP.getSelectedItemPosition();		
		searchCode=(proviceMap.get("proviceCodes")[t]).equals("000000")?"":proviceMap.get("proviceCodes")[t].substring(0,2);
		searchTitle=((EditText)findViewById(R.id.word_collection_title)).getText().toString();
		pager.setCurrentPage(1);
		search();
	}
	
	public void search(){		
		dbOperator=new DatabaseOperation(this);			
		String limit = ((pager.getCurrentPage() - 1) * Constants.PAGE_NUM) + ","+ Constants.PAGE_NUM;
		Cursor cursor =dbOperator.query("word",  new String[]{"title","word_id"}, "city_id like ? and title like ? and user_id =?",  new String[]{searchCode+"%","%"+searchTitle+"%",Constants.USER_Mobile}, null,null, "word_id desc", limit);
		list=new ArrayList();		
		while (cursor.moveToNext()) {	
			Map map=new HashMap();
			map.put("title", cursor.getString(cursor.getColumnIndex("title")));
			map.put("wordId", cursor.getString(cursor.getColumnIndex("word_id")));
			list.add(map);
		}
		
		cursor =dbOperator.query("word",  new String[]{"count(word_id) c"}, "city_id like ? and title like ? and user_id =?",  new String[]{searchCode+"%","%"+searchTitle+"%",Constants.USER_Mobile}, null, null, null,null);
		cursor.moveToFirst();
		int k=cursor.getInt(cursor.getColumnIndex("c"));		
		dbOperator.closeDb(cursor);	
		showList(k);
	}

	private void showList(int totlaNum) {	
		pager=new Pager(totlaNum,pager.getCurrentPage());
		if(totlaNum==0){
			emptyTV.setText("暂无数据！");
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this,list,R.layout.common_list_item1, new String[] { "title"}, new int[] {R.id.common_list_item1_tv1});
		this.setListAdapter(simperAdapter);				
	}
	
	
	public void onException(final Exception e) {
		WordCollectionActivity.this.runOnUiThread(new Runnable() {
			public void run() {
				if (proDialog != null) {
					proDialog.dismiss();
				}
				emptyTV.setText(e.getMessage());							
			}
		});
	}

	
	protected void onListItemClick(ListView l, View v, int position, long id) {	
		Intent intent = new Intent();	
		intent.setClass(this, WordDetailActivity.class);
		intent.putExtra("showType","2");
		intent.putExtra("wordId", ((Map) list.get(position)).get("wordId").toString());		
		startActivity(intent);
	}
	
	
	
	private void setGV() {
		gv = (GridView) this.findViewById(R.id.word_collection_gv);
		gv.setNumColumns(2);
		gv.setAdapter(getMenuAdapter(menu_text_array, menu_image_array));
		gv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {			
				switch (arg2) {
				case 0:
					if (1 == pager.getCurrentPage()) {// 上一页置为无效
						Toast.makeText(WordCollectionActivity.this, "当前页为第一页",Toast.LENGTH_SHORT).show();
					}else{
						pager.doProvepage();
						search();
					}							
					break;
				case 1:
					if (pager.getTotalPage() == pager.getCurrentPage() || pager.getTotalPage() == 0) {
						Toast.makeText(WordCollectionActivity.this, "当前页为最后一页",Toast.LENGTH_SHORT).show();
					}else{
						pager.doNextpage();
						search();
					}
					break;
				}
			}
		});
	}
	
}
